﻿using System;
using System.ComponentModel;
using System.Globalization;
using Loki.Commands;
using Loki.UI;
using Loki.Utils;
using Luna.Model.CRM;
using Luna.UI.Resources;

namespace Luna.UI.CRM
{
    public class AccountEditViewModel : LunaViewModel, IDataErrorInfo
    {
        #region CurrentAccount

        private static PropertyChangedEventArgs _CurrentAccountChangedArgs = ObservableHelper.CreateChangedArgs<AccountEditViewModel>(x => x.CurrentAccount);

        private static PropertyChangingEventArgs _CurrentAccountChangingArgs = ObservableHelper.CreateChangingArgs<AccountEditViewModel>(x => x.CurrentAccount);

        private IAccount _CurrentAccount;

        public IAccount CurrentAccount
        {
            get
            {
                return _CurrentAccount;
            }

            set
            {
                if (value != _CurrentAccount)
                {
                    NotifyChanging(_CurrentAccountChangingArgs);
                    _CurrentAccount = value;
                    NotifyChanged(_CurrentAccountChangedArgs);
                }
            }
        }

        #endregion CurrentAccount

        #region Name

        private static PropertyChangedEventArgs _NameChangedArgs = ObservableHelper.CreateChangedArgs<AccountEditViewModel>(x => x.Name);

        private static PropertyChangingEventArgs _NameChangingArgs = ObservableHelper.CreateChangingArgs<AccountEditViewModel>(x => x.Name);

        private string _Name;

        public string Name
        {
            get
            {
                return _Name;
            }

            set
            {
                if (value != _Name)
                {
                    NotifyChanging(_NameChangingArgs);
                    _Name = value;
                    NotifyChangedAndDirty(_NameChangedArgs);
                }
            }
        }

        #endregion Name

        #region Address

        private static PropertyChangedEventArgs _AddressChangedArgs = ObservableHelper.CreateChangedArgs<AccountEditViewModel>(x => x.Address);

        private static PropertyChangingEventArgs _AddressChangingArgs = ObservableHelper.CreateChangingArgs<AccountEditViewModel>(x => x.Address);

        private string _Address;

        public string Address
        {
            get
            {
                return _Address;
            }

            set
            {
                if (value != _Address)
                {
                    NotifyChanging(_AddressChangingArgs);
                    _Address = value;
                    NotifyChangedAndDirty(_AddressChangedArgs);
                }
            }
        }

        #endregion Address

        #region ZipCode

        private static PropertyChangedEventArgs _ZipCodeChangedArgs = ObservableHelper.CreateChangedArgs<AccountEditViewModel>(x => x.ZipCode);

        private static PropertyChangingEventArgs _ZipCodeChangingArgs = ObservableHelper.CreateChangingArgs<AccountEditViewModel>(x => x.ZipCode);

        private string _ZipCode;

        public string ZipCode
        {
            get
            {
                return _ZipCode;
            }

            set
            {
                if (value != _ZipCode)
                {
                    NotifyChanging(_ZipCodeChangingArgs);
                    _ZipCode = value;
                    NotifyChangedAndDirty(_ZipCodeChangedArgs);
                }
            }
        }

        #endregion ZipCode

        #region City

        private static PropertyChangedEventArgs _CityChangedArgs = ObservableHelper.CreateChangedArgs<AccountEditViewModel>(x => x.City);

        private static PropertyChangingEventArgs _CityChangingArgs = ObservableHelper.CreateChangingArgs<AccountEditViewModel>(x => x.City);

        private string _City;

        public string City
        {
            get
            {
                return _City;
            }

            set
            {
                if (value != _City)
                {
                    NotifyChanging(_CityChangingArgs);
                    _City = value;
                    NotifyChangedAndDirty(_CityChangedArgs);
                }
            }
        }

        #endregion City

        #region Country

        private static PropertyChangedEventArgs _CountryChangedArgs = ObservableHelper.CreateChangedArgs<AccountEditViewModel>(x => x.Country);

        private static PropertyChangingEventArgs _CountryChangingArgs = ObservableHelper.CreateChangingArgs<AccountEditViewModel>(x => x.Country);

        private string _Country;

        public string Country
        {
            get
            {
                return _Country;
            }

            set
            {
                if (value != _Country)
                {
                    NotifyChanging(_CountryChangingArgs);
                    _Country = value;
                    NotifyChangedAndDirty(_CountryChangedArgs);
                }
            }
        }

        #endregion Country

        #region PhoneNumber

        private static PropertyChangedEventArgs _PhoneNumberChangedArgs = ObservableHelper.CreateChangedArgs<AccountEditViewModel>(x => x.PhoneNumber);

        private static PropertyChangingEventArgs _PhoneNumberChangingArgs = ObservableHelper.CreateChangingArgs<AccountEditViewModel>(x => x.PhoneNumber);

        private string _PhoneNumber;

        public string PhoneNumber
        {
            get
            {
                return _PhoneNumber;
            }

            set
            {
                if (value != _PhoneNumber)
                {
                    NotifyChanging(_PhoneNumberChangingArgs);
                    _PhoneNumber = value;
                    NotifyChangedAndDirty(_PhoneNumberChangedArgs);
                }
            }
        }

        #endregion PhoneNumber

        #region Url

        private static PropertyChangedEventArgs _UrlChangedArgs = ObservableHelper.CreateChangedArgs<AccountEditViewModel>(x => x.Url);

        private static PropertyChangingEventArgs _UrlChangingArgs = ObservableHelper.CreateChangingArgs<AccountEditViewModel>(x => x.Url);

        private string _Url;

        public string Url
        {
            get
            {
                return _Url;
            }

            set
            {
                if (value != _Url)
                {
                    NotifyChanging(_UrlChangingArgs);
                    _Url = value;
                    NotifyChangedAndDirty(_UrlChangedArgs);
                }
            }
        }

        #endregion Url

        #region Mail

        private static PropertyChangedEventArgs _MailChangedArgs = ObservableHelper.CreateChangedArgs<AccountEditViewModel>(x => x.Mail);

        private static PropertyChangingEventArgs _MailChangingArgs = ObservableHelper.CreateChangingArgs<AccountEditViewModel>(x => x.Mail);

        private string _Mail;

        public string Mail
        {
            get
            {
                return _Mail;
            }

            set
            {
                if (value != _Mail)
                {
                    NotifyChanging(_MailChangingArgs);
                    _Mail = value;
                    NotifyChangedAndDirty(_MailChangedArgs);
                }
            }
        }

        #endregion Mail

        public AccountEditViewModel()
        {
            Init += AccountEditViewModel_Init;
            Load += AccountEditViewModel_Load;
        }

        private void AccountEditViewModel_Load(object P_Sender, EventArgs e)
        {
            RefreshValues();
        }

        private void AccountEditViewModel_Init(object P_Sender, EventArgs e)
        {
            this.WatchChange(this, x => x.CurrentAccount, c => c.CurrentAccount_Changed);
            Commands.Handle(Loki.Commands.ApplicationCommands.SAVE, Application_Save_CanExecute, Application_Save_Execute);

            AddValidator<AccountEditViewModel, string>(x => x.Name, Rules.Required, "Le nom est requis.");
            AddValidator<AccountEditViewModel, string>(x => x.Address, Rules.Required, "L'addresse est requise");
        }

        private void CurrentAccount_Changed(object P_Sender, PropertyChangedEventArgs e)
        {
            RefreshValues();
        }

        private void RefreshValues()
        {
            DisplayName = string.Format(CultureInfo.CurrentUICulture, FormNames.ACCOUNT_EDIT, CurrentAccount.Name);

            /*Name = CurrentAccount.Name;
            Address = CurrentAccount.Address;
            ZipCode = CurrentAccount.Zipcode;
            City = CurrentAccount.City;
            Country = CurrentAccount.Country;
            Url = CurrentAccount.Url;*/

            Validate(ValidationMode.Information);
        }

        private void Application_Save_CanExecute(object P_Sender, CanExecuteCommandEventArgs e)
        {
            e.CanExecute |= IsChanged;
        }

        private void Application_Save_Execute(object P_Sender, CommandEventArgs e)
        {
            /*if (Validate())
            {
                CurrentAccount.Name = Name;
                CurrentAccount.Address = Address;
                CurrentAccount.Zipcode = ZipCode;
                CurrentAccount.City = City;
                CurrentAccount.Country = Country;
                CurrentAccount.Url = Url;

                ApplicationModel.Contacts.Save(CurrentAccount);

                CurrentAccount = ApplicationModel.Contacts.Accounts[CurrentAccount.ID];

                UserMessage(Resources.Messages.SAVE_GENERIC);
            }*/
        }
    }
}